//
//  WelcomeViewController.swift
//  NSinaWeibo
//
//  Created by LoliQueen on 16/2/19.
//  Copyright © 2016年 LoliQueen. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WelcomeViewController: UIViewController {
    
    override func loadView() {
        view = UIImageView(image: UIImage(named: "ad_background"))
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        
    }
    
    //根视图一般会在viewWillLayoutsubViews，重新设置，所以东湖效果一般写在viewWillLayoutsubViews，不要写在viewdidiload中
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        iconAnimation()
    }
    
    //懒加载用户头像和文字
    private lazy var iconImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //文字
    private lazy var welcomeLabel:UILabel = UILabel(title: (AccountViewModel.shareAccountViewModel.userName ?? "") + " 欢迎归来", textSize: 18, textColor: UIColor.darkGrayColor())
    
    
    //加载子控件
    private func setUI(){
        view.addSubview(iconImageView)
        view.addSubview(welcomeLabel)
        
        //设置子控件约束
        iconImageView.snp_makeConstraints(closure: { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-160)
            //设置头像大小
            make.size.equalTo(CGSizeMake(90, 90))
        })
        
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImageView.snp_centerX)
            make.top.equalTo(iconImageView.snp_bottom).offset(16)
        }
        
        //隐藏label，等头像的动画效果结束后显示
        welcomeLabel.alpha = 0
        
        //获得用户头像图片
        iconImageView.sd_setImageWithURL(AccountViewModel.shareAccountViewModel.userImageUrl)
        print(AccountViewModel.shareAccountViewModel.userImageUrl)
        //设置呈圆形
        iconImageView.layer.cornerRadius = 45
        iconImageView.layer.masksToBounds = true
        
        
    }
    
    //设置头像的动画效果
    func iconAnimation(){
        
        let offset = -UIScreen.mainScreen().bounds.height + 160
        //更新头像的位置
        self.iconImageView.snp_updateConstraints(closure: { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(offset)
        })
        //修改约束  意味底部有两条约束
        //snp_updateConstraints 更新一条相同的约束
        //当前阶段只是 '收集' 了约束的变化 并没有重新布局页面
        
        UIView.animateWithDuration(1.6, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            
            //强制更新页面，提前更新页面，必须写在闭包中
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                //动画结束显示文字
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        
                        //欢迎界面加载完成之后，跳转到主界面
                        //不推荐这种写法
//                        UIApplication.sharedApplication().keyWindow?.rootViewController = MainViewController()
                        //推荐 使用通知
                        NSNotificationCenter.defaultCenter().postNotificationName(KChooseRootVC, object: "welcome")
                        print("OK")
                })
        }
        
        
    }

    
    
    
    
}
